$.extend({
    pager: function (pc,ps,size,url) {
        size = size ? size : 4;
        setPage(pc,ps,size,url);
    }
})

function setPage(pc,ps,size,url){
    pc = parseInt(pc);
    ps = parseInt(ps);
    var head,tail;
    head = pc;
    tail = head+size;
    var page = $("#pc");
    page.html("<li id='headpage'><a href='#' aria-label='headpage'>首页</a></li><li id='prepage'><a href='#' aria-label='Previous'>上一页</a></li>");
    var currsize = size;

    if(size>ps){
        head = 1;
        tail = ps;
    }else if(pc-size/2<head||pc+size/2>tail){
        head = pc - size/2;
        tail = pc + size/2;
        if(head<1){
            head = 1;
            tail = head + size;
        }
        else if(tail>ps){
            tail = ps;
            head = tail - size;
        }
    }
    for(var i=head;i<=tail;i++){
        var li = $("<li></li>");
        if(i==pc)
            li.addClass("active");
        var a = $("<a></a>");
        a.addClass("pagenum");
        a.text(i);
        li.append(a);
        page.append(li);
    }
    if(tail!=ps){
        var li = $("<li></li>");
        var a = $("<p></p>");
        a.text("...");
        li.append(a);
        page.append(li);
    }
    var ht = page.html();
    page.html(ht+"<li id='nextpage'><a href='#' aria-label='Next'>下一页</a></li><li id='endpage'><a href='#' aria-label='endpage'>末页</a></li>"+
        "<li><p>共"+ps+"页</p></li><li><p>当前</p><input type='text' class='input-sm' id='jumpinput' placeholder="+pc+">"+
        "<p>页</p></li><li><a href='#' aria-label='jump'>确定</a></li>");


    if(pc==1){
        $("#headpage").addClass("disabled");
        $("#prepage").addClass("disabled");
    }
    if(pc==ps){
        $("#nextpage").addClass("disabled");
        $("#endpage").addClass("disabled");
    }


    $("a[aria-label='Previous']").click(function () {
        if(pc!=1)
            pc=pc-1;
        setPage(pc,ps,size,url);
    });

    $("a[aria-label='Next']").click(function () {
        if(pc!=ps)
            pc=++pc;
        setPage(pc,ps,size,url);
    });

    $("a[aria-label='headpage']").click(function () {
        pc=1;
        setPage(pc,ps,size,url);
    });

    $("a[aria-label='endpage']").click(function () {
        pc=ps;
        setPage(pc,ps,size,url);
    });

    $("a[aria-label='jump']").click(function () {
        pc = $('#jumpinput').val();
        setPage(pc,ps,size,url);
    });

    $(".pagenum").click(function(){
        pc = $(this).text();
        setPage(pc,ps,size,url);
    });

    $(".pagination li a").click(function () {
        var lin = "?";
        if(url.indexOf('?')>=0)
            lin = "&";
        var hrurl = url+lin+"pc="+pc+"&ps="+ps;
        window.location.href= hrurl ;
    });
}

